package com.pd.modules.client;

import com.pd.modules.common.ConnectionStatus;
import com.pd.modules.common.Message;

public class Client
{
	private Thread mServerConnectionThread;//
	private ServerConnectionThread mServerConnection;

	private Client()
	{
	}

	/**
	 * SingletonHolder is loaded on the first execution of
	 * Singleton.getInstance() or the first access to SingletonHolder.INSTANCE,
	 * not before.
	 */
	private static class SingletonHolder
	{
		public static final Client instance = new Client();
	}

	public static Client getInstance()
	{
		return SingletonHolder.instance;
	}

	public void connectToServer(String _IP, int _port, String _username, String _password, boolean _useMulticast)
	{
		System.out.println("Creating connection thread...");

		mServerConnection = new ServerConnectionThread(_IP, _port, _username, _password, _useMulticast);
		mServerConnectionThread = new Thread(mServerConnection);
		mServerConnectionThread.setDaemon(true);
		mServerConnectionThread.start();
	}

	public void sendMessageToServer(Message _message, boolean _encrypt)
	{
		mServerConnection.sendMessage(_message, _encrypt);
	}

	public void stop()
	{
		mServerConnection.disconnect(false);
	}

	public String getUsername()
	{
		return mServerConnection.getUsername();
	}
	
	public boolean isConnectedToServer()
	{
		return null == mServerConnection ? false : mServerConnection.getConnectionStatus() == ConnectionStatus.Connected;
	}
}